/**
 * 
 */
package com.krt.game01;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;


public class Level {

	ArrayList<Button> m_buttonList = new ArrayList<Button>();
	ArrayList<Button> m_buttonChain = new ArrayList<Button>();
	Button m_plusButton = new Button();
	Button m_minusButton = new Button();
	Button m_chainButton = new Button();

	Context m_appContext;

	int m_numButtons = 0;
	int m_buttonWidth = 0;
	int m_buttonHeight = 0;

	//padding in percent
	int m_paddingTop = 10;
	int m_paddingBottom = 10;
	int m_paddingHorizontal = 5;
	int m_paddingVertical = 5;

	int m_buttonsPerLine = 4;

	float m_paddingHorAbs = 0;
	float m_paddingTopAbs = 0;
	float m_paddingBotAbs = 0;
	float m_paddingVerAbs = 0;

	public Level() {
		// TODO Auto-generated constructor stub
	}

	public Level(Context context, int viewWidth, int viewHeight, int ... numbers){
		m_appContext = context;
		m_numButtons = numbers.length;
		//		int tLine = numbers.length/m_buttonsPerLine;
		//		Log.d("createLevel", "tLine = " + tLine);

		m_paddingHorAbs = viewWidth *((float)m_paddingHorizontal/100);
		m_paddingTopAbs = viewHeight*((float)m_paddingTop/100);
		m_paddingBotAbs = viewHeight*((float)m_paddingBottom/100);
		m_paddingVerAbs = viewHeight*((float)m_paddingVertical/100);

		//subtract padding on the left and on the right
		int spaceX = (int)(viewWidth - (float)2*m_paddingHorAbs);


		//subtract padding on top and bottom
		int spaceY = (int)(viewHeight - (float)m_paddingTopAbs - (float)m_paddingBotAbs);

		Log.d("createLevel", "viewWidth remaining = " + spaceX);
		Log.d("createLevel", "viewHeight remaining = " + spaceY);

		//assuming width < height = spaceX < spaceY

		m_buttonWidth = (int)(((float)spaceX 	//remaining space x
				- (m_buttonsPerLine - 1) * m_paddingHorAbs)	//subtract padding between buttons
				/ m_buttonsPerLine);	//divide by buttons per line

		Log.d("createLevel", "m_buttonWidth = " + m_buttonWidth);

		m_buttonHeight = m_buttonWidth;	//we want squares for now
		Log.d("createLevel", "numbers = " + m_numButtons);

		int tRow = 0, tLine = 0, tCounter = 0;
		for (int x = 0; x < numbers.length; x++){
			tRow = x % m_buttonsPerLine;

			addButton(tRow, tLine, numbers[x]);

			if (tCounter == 3){
				tCounter = 0;
				tLine++;
			}
			else if (tCounter < 3) tCounter++;

		}
		float tempwidth = (viewWidth - (4*m_paddingHorAbs)) / 3;
		m_plusButton.setCoords((int)m_paddingHorAbs, viewHeight-100, (int)(m_paddingHorAbs + tempwidth), viewHeight);
		m_plusButton.m_isSpecial = true;
		m_plusButton.m_text = "plus";

		m_minusButton.setCoords((int)(2*m_paddingHorAbs + tempwidth), viewHeight-100, (int)(2*m_paddingHorAbs + 2*tempwidth), viewHeight);
		m_minusButton.m_isSpecial = true;
		m_minusButton.m_text = "minus";

		m_chainButton.setCoords((int)(3*m_paddingHorAbs + 2*tempwidth), viewHeight-100, (int)(3*m_paddingHorAbs + 3*tempwidth), viewHeight);
		m_chainButton.m_isSpecial = true;
		m_chainButton.m_text = "chain";

	}

	//possible grid = m_buttonsPerLine x m_buttonsPerLine
	//CAUTION: origin = top left
	public void addButton(int x, int y, int value) {
		//Button left, top, right, bottom, number
		m_buttonList.add(new Button(
				(int)m_paddingHorAbs*(x+1) + m_buttonWidth*(x),	//left 
				(int)m_paddingTopAbs + (int)m_paddingVerAbs*(y) + m_buttonHeight*y,		//top
				(int)m_paddingHorAbs*(x+1) + m_buttonWidth*(x+1),//right
				(int)m_paddingTopAbs + (int)m_paddingVerAbs*(y) + m_buttonHeight*(y+1),	//bottom
				value));	

	}

	public ArrayList<Button> getButtonList(){
		return m_buttonList;		
	}

	private void chainCleanup(){
		m_buttonChain.clear();

		for(int i=1; i<=m_buttonList.size(); i++){
			m_buttonList.get(i-1).setTouched(false);
		}
		m_plusButton.setTouched(false);
		m_minusButton.setTouched(false);
		m_chainButton.setTouched(false);
	}

	public boolean checkCurrentChain() {
		Log.d("checkCurrentChain", "size = " + m_buttonChain.size());

		//only 3 buttons --> add or subtract values
		//we assume: number/plus or minus/number
		if (m_buttonChain.size() == 3) {
			//if number buttons are the same then display error, clean up and return false
			if (m_buttonChain.get(0) == m_buttonChain.get(2)) {
				Toast toast = Toast.makeText(m_appContext,
						"Operation on same button now allowed", Toast.LENGTH_LONG);
				toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);			
				toast.show();
				chainCleanup();
				return false;
			}
			//addition
			if (m_buttonChain.get(1).m_text == "plus") {
				Log.d("checkCurrentChain", "addition = " + m_buttonChain.get(0).getValue() + m_buttonChain.get(2).getValue());

				m_buttonChain.get(2).setValue(m_buttonChain.get(0).getValue() + m_buttonChain.get(2).getValue());	//add button values
				m_buttonChain.get(0).setVisibility(false);
				chainCleanup();
				return true;
			}

			//subtraction
			if (m_buttonChain.get(1).m_text == "minus") {

				if (MainActivity.NEGATIVEALLOWED == false) {
					if (m_buttonChain.get(2).getValue() > m_buttonChain.get(0).getValue()) {
						Toast toast = Toast.makeText(m_appContext,
								"Second value is bigger than first value", Toast.LENGTH_LONG);
						toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);			
						toast.show();
						chainCleanup();
						return false;
					}
				}
				Log.d("checkCurrentChain", "subtraction = " + (m_buttonChain.get(0).getValue()-m_buttonChain.get(2).getValue()));
				
				if (m_buttonChain.get(0).getValue() == m_buttonChain.get(2).getValue()) {
					m_buttonChain.get(0).setVisibility(false);		//only setVisibility(false) for first button because the rest is done after if block
				}
				
				m_buttonChain.get(0).setValue(m_buttonChain.get(0).getValue() - m_buttonChain.get(2).getValue());	//subtract button values
				m_buttonChain.get(2).setVisibility(false);
				chainCleanup();
				return true;


			}


		}




		//		if (m_buttonChain.size() == 2) {	//only 2 buttons --> add or subtract values
		//
		//			//first value is smaller than second value --> add
		//			if (m_buttonChain.get(0).getValue() < m_buttonChain.get(1).getValue()) {
		//				m_buttonChain.get(1).setValue(m_buttonChain.get(0).getValue() + m_buttonChain.get(1).getValue());
		//				m_buttonChain.get(0).setVisibility(false);
		//				m_buttonChain.get(0).setTouched(false);
		//				m_buttonChain.get(1).setTouched(false);
		//			}
		//			//first value is bigger than second value --> subtract
		//			else if (m_buttonChain.get(0).getValue() > m_buttonChain.get(1).getValue()) {
		//				m_buttonChain.get(0).setValue(m_buttonChain.get(0).getValue() - m_buttonChain.get(1).getValue());
		//				m_buttonChain.get(1).setVisibility(false);
		//				m_buttonChain.get(0).setTouched(false);
		//				m_buttonChain.get(1).setTouched(false);
		//			}
		//			//first value is equal than second value --> subtract and make invisible
		//			else if (m_buttonChain.get(0).getValue() == m_buttonChain.get(1).getValue()) {
		//				m_buttonChain.get(1).setValue(m_buttonChain.get(0).getValue() - m_buttonChain.get(1).getValue());
		//				m_buttonChain.get(0).setVisibility(false);
		//				m_buttonChain.get(1).setVisibility(false);
		//				m_buttonChain.get(0).setTouched(false);
		//				m_buttonChain.get(1).setTouched(false);
		//			}
		//
		//
		//		}

		//Check if chain is longer than 2 buttons
		//check if chain is valid:
		//	a) first value is NOT 1
		//	b) every button value is a multiple of the first button value
		//	c) each button value is higher or equal than the value of the previous button value 
		//		if (m_buttonChain.size() > 2) {	//3 buttons or more --> 
		//			int firstValue = m_buttonChain.get(0).getValue();
		//			boolean chainIsValid = true;
		//			int lastValue = 0;
		//
		//			if (firstValue != 1) {
		//				for(int i=1; i<=m_buttonChain.size(); i++){
		//					Log.d("buttonChain iteration", "modulo = " + m_buttonChain.get(i-1).getValue() % firstValue);
		//
		//					if (m_buttonChain.get(i-1).getValue() < lastValue) {		//is current value not higher than last value?
		//						chainIsValid = false;
		//						Toast toast = Toast.makeText(m_appContext,
		//								"Values are not in ascending order!", Toast.LENGTH_LONG);
		//						toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);			
		//						toast.show();	
		//						break;
		//					}
		//					if (m_buttonChain.get(i-1).getValue() % firstValue != 0) {		//is value not a multiple of first value?
		//						chainIsValid = false;
		//						Toast toast = Toast.makeText(m_appContext,
		//								m_buttonChain.get(i-1).getValue() + " is not a multiple of " + firstValue + "!", Toast.LENGTH_LONG);
		//						toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);			
		//						toast.show();
		//						break;
		//					}
		//
		//					lastValue = m_buttonChain.get(i-1).getValue();
		//				}
		//				Log.d("checkCurrentChain", "chainIsValid = " + chainIsValid);
		//
		//
		//
		//				if (chainIsValid) {
		//					for(int i=1; i<=m_buttonChain.size(); i++){
		//						m_buttonChain.get(i-1).setVisibility(false);
		//						m_buttonChain.get(i-1).setTouched(false);
		//					}
		//				}
		//
		//			}
		//			else if (firstValue == 1) {
		//				Toast toast = Toast.makeText(m_appContext, "Chain must not start with 1!", Toast.LENGTH_LONG);
		//				toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);			
		//				toast.show();			
		//			}
		//		}



		//		for(int i=1; i<=m_buttonChain.size(); i++){
		//			m_buttonChain.get(i-1).setVisibility(false);
		//		}

		chainCleanup();

		return false;
	}

}
